package com.cx.anno;

import java.lang.reflect.Method;

public class demo5_16 {
    public static void main(String[] args) throws Exception {
        // 创建对象，检查对象的空字段
        User user = new User();
        user.setUserName("admin");
        //1 检查类是否为日志类，是否加了Logger注解
        Class<?> clazz = User.class;
        if(clazz.isAnnotationPresent(Logger.class)){
            Logger logger = clazz.getAnnotation(Logger.class);
            System.out.println(logger.value() + " " + logger.level() + ":");
        }else{
            throw new Exception("该对象不是日志类");
        }
        // 2 检查是否有异常输出，方法是否加了ExceptionHandler注解
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            if(method.isAnnotationPresent(ExceptionHandler.class)){
                ExceptionHandler handler = method.getAnnotation(ExceptionHandler.class);
                System.out.println(handler.value() + " " + handler.message() + ":");
                // 3 执行方法，检查对象字段是否空
                method.invoke(user);
            }
        }
    }
}